/*************************************************************************
	> File Name: single.h
	> Author: ma6174
	> Mail: ma6174@163.com 
	> Created Time: 2021年12月06日 星期一 18时14分31秒
 ************************************************************************/

#include<iostream>
#include <string>
#include <mutex>

class Singleton
{
public:
	~Singleton()
	{
		delete instance;
	}

	static Singleton* getInstance(std::string s);

	void printInstance()
	{
		std::cout << eg << std::endl;
	}

private:
	static Singleton* instance;
	std::string eg;

	Singleton(std::string s) : eg(s) {}
};

Singleton* Singleton::instance = nullptr;

Singleton* Singleton::getInstance(std::string s)
{
	//双重锁定
	std::mutex valMutex;
	if (instance == nullptr)
	{
		valMutex.lock();
		if (instance == nullptr)
			instance = new Singleton(s);
		valMutex.unlock();
	}
	return instance;
}
